<?php

FLEA::loadClass('Common_Model');

class Model_Articles extends Common_Model {

	private $_tableArticle;
	private $_ui;
	private $_modelCatalog;
	private $allCatalogs;

	function __construct() {
		$this->_tableArticle = FLEA::getSingleton('Table_Articles');
		
		//取得文章的栏目
		$this->_modelCatalog = FLEA::getSingleton('Model_ArticleCatalog');
		$this->allCatalogs = $this->_modelCatalog->getAllCatalogs('id2name');
	}
	
	function getAllArticles() {
		$viewData['data'] = $this->renderList($this->_tableArticle->findAll(null,'article_id DESC'));
		return $viewData;
	}

	function getTitlesByCatalog($catalogId, $recordNumber = 15) {
	
		$fields = ' article_id, catalog_id, title, created ';
		$condition = ' catalog_id = '.$catalogId;
		$recordNumber = (int)$recordNumber;
		$strSql = 'SELECT TOP '.$recordNumber.$fields.'FROM articles WHERE'.$condition;
		
		$viewData['titles'] = $this->_tableArticle->findBySql($strSql);
		return $viewData;
	}

	function getArticleById($articleId) {
		$result = $this->_tableArticle->find('article_id = ' . $articleId);
		return $result;
	}

	function insertArticle($row) {
		$result = $this->_tableArticle->create($row);
		return $result;
	}
	
	function updateArticle($row) {
		$result = $this->_tableArticle->update($row);
		return $result;
	}

	function deleteArticleById($articleId) {
		$result = $this->_tableArticle->removeByPkv($articleId);
		return $result;
	}
	
function renderList($listData) {
		//require_once 'HTML/Table.php';
		$tableFacade = FLEA::getSingleton('Common_TableFacade');
		//set table attributes
		$attributes = array("width"=>"100%", "border"=>"0");

		//create the table class
		$table = new $tableFacade($attributes);
		
		$table->setCaption('文章列表&nbsp;&nbsp;&nbsp;&nbsp;<a href=' . url('adminarticle', 'insert') . '>添加新文章</a>');
		//$table->setRowCount(10);
		
		//build our first row
		$contents = array("ID", "TITLE", "CREATED", "CATALOG", "OPERATION");
		$attributes = array('bgcolor'=>"#336699");
		$table->addRow($contents, $attributes, "TH");
		
		//loop through and add our data
		$attributes = array('bgcolor'=>"#COCOCO");

		foreach ($listData as $data) {
			$strOperation = '<a href=\'' . url ( 'adminarticle', 'modify', array ('article' => $data ['article_id'] ) ) . '\'>修改</a>&nbsp;&nbsp;<a href=\'' . url ( 'adminarticle', 'delete', array ('article' => $data ['article_id'] ) ) . '\'>删除</a>';
			$contents = array($data['article_id'], $data['title'], $data['created'], $this->allCatalogs[$data['catalog_id']], $strOperation);
			$table->addRow($contents, $attributes);
		}

		return $table->toHTML();
	}
	
	
	//生成添加文章的表单元素
	function getForm($data) {
		$this->_ui = FLEA::initWebControls();
		$form = array(
			'article_id' => $this->_ui->control('Hidden', 'article_id',
				array(
					'value' => $data['article_id']
				),
				true
			),
			'catalog_id' => $this->_ui->control('DropdownList', 'catalog_id',
				array(
					'required' => 'true',
					'selected' => empty($data['catalog_id']) ? -1 : $data['catalog_id'],
					'items' => $data['catalogs'],
					'showName' => '文章栏目'
				),
				true
			),
			'title' => $this->_ui->control('Textbox', 'title',
				array(
					'value' => $data['title'],
					'required' => 'true',
					'check' => 'notBlank',
					'size' => 98,
					'showName' => '文章标题'
				),
				true
			),
			'author' => $this->_ui->control('Textbox', 'author',
				array(
					//'required' => 'true',
					'value' => $data['author'],
					'showName' => '作者',
					'readonly' => 'true'
				),
				true
			),
			'content' => $this->_ui->control('Fckeditor', 'content',
				array(
					'required' => 'true',
					'Value' => $data['content'],
					'showName' => '新闻内容'
				),
				true
			),
			'deadline' => $this->_ui->control('Textbox', 'deadline',
				array(
					'check' => 'isDate(\'YYYY-MM-DD\');',
					'value' => date('Y-m-d', time()+3600*24*7),
					'showName' => '有效日期'
				),
				true
			),
			'click' => $this->_ui->control('Textbox', 'click',
				array(
					'value' => $data['click'],
					'showName' => '阅读次数'
				),
				true
			),
			'important' => $this->_ui->control('Checkbox', 'important',
				array(
					'value' => 1,
					'checked' => $data['important']
				),
				true
			)
		);
		return $form;
	}

}